package com.atguigu.jxc.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.jxc.dao.GoodsDao;
import com.atguigu.jxc.dao.PurchaseListDao;
import com.atguigu.jxc.dao.PurchaseListGoodsDao;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.entity.PurchaseList;
import com.atguigu.jxc.entity.PurchaseListGoods;
import com.atguigu.jxc.entity.User;
import com.atguigu.jxc.service.PurchaseListService;
import com.atguigu.jxc.util.AuthContextHolder;
import com.atguigu.jxc.vo.PurchaseListAndReturnListVo;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PurchaseListServiceImpl implements PurchaseListService {


    @Autowired
    private PurchaseListDao purchaseListDao;

    @Autowired
    private PurchaseListGoodsDao purchaseListGoodsDao;

    @Autowired
    private GoodsDao goodsDao;


    //新增进货单
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void savePurchaseList(PurchaseList purchaseList, String purchaseListGoodsStr, HttpSession session) {
        //获取用户信息
        User userInfo = AuthContextHolder.getUserInfo(session);
        //保存进货单
        purchaseList.setUserId(userInfo.getUserId());
        purchaseList.setTrueName(userInfo.getTrueName());
        purchaseListDao.savePurchaseList(purchaseList);
        //将string转为对象
        List<PurchaseListGoods> purchaseListGoods = JSONObject.parseArray(purchaseListGoodsStr, PurchaseListGoods.class);
        //遍历
        purchaseListGoods.forEach(item -> {
            item.setPurchaseListId(purchaseList.getPurchaseListId());
            //保存进货明细
            purchaseListGoodsDao.savePurchaseListGoods(item);
            //进货单添加对应添加商品库存
            Goods goods =goodsDao.getGoodsById(item.getGoodsId());
            goods.setInventoryQuantity(goods.getInventoryQuantity()+item.getGoodsNum());
            goods.setState(1);
            goodsDao.updateGoods(goods);
        });
    }


    //进货单列表展示（可条件查询：单据号模糊、供应商、是否付款和日期区间）
    @Override
    public Map<String, Object> list(String purchaseNumber, Integer supplierId, Integer state, String sTime, String eTime) {
        //查询
        List<PurchaseList> purchaseListList=purchaseListDao.listSelect(purchaseNumber,supplierId,state,sTime,eTime);
        Map<String, Object> map= new HashMap<>();
        map.put("rows",purchaseListList);
        return map;
    }

    //查询进货单商品信息  根据purchaseListId
    @Override
    public Map<String, Object> goodsList(Integer purchaseListId) {
        //查询
        List<PurchaseListGoods> purchaseListGoodsList=purchaseListGoodsDao.goodsList(purchaseListId);
        Map<String, Object> map= new HashMap<>();
        map.put("rows",purchaseListGoodsList);
        return map;
    }

    //进货单删除
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(Integer purchaseListId) {
        if (purchaseListId!=null){
            //删除进货明细
            purchaseListGoodsDao.deletePurchaseListGoods(purchaseListId);
            //删除进货单
            purchaseListDao.deletePurchaseList(purchaseListId);
        }
    }

    //支付结算（修改进货单付款状态） Integer purchaseListId
    @Override
    public void updateState(Integer purchaseListId) {
        if (purchaseListId!=null){
            //修改
            //根据id查询付款状态
            PurchaseList purchaseList =purchaseListDao.selectById(purchaseListId);
            purchaseList.setState(1);
            purchaseListDao.updateState(purchaseList);
        }
    }

    //进货统计（可根据 商品类别、商品编码或名称 条件查询）
    @Override
    public String count(String sTime, String eTime, Integer goodsTypeId, String codeOrName) {
        //PurchaseListGoods     PurchaseList  t_supplier   t_goods_type
        List<PurchaseListAndReturnListVo> purchaseListVoList=purchaseListDao.count(sTime,eTime,goodsTypeId,codeOrName);
        Gson gson = new Gson();
        //把list转为json
        String json = gson.toJson(purchaseListVoList);
        return json;
    }
}
